XS Cut point reporter
Description
-
This is a custom interactive tool to generate corridors XSCutPoints data.
-
ReportAllXSCutPoints () methods generates data for corridor selected by user.
-
ReportAllXSCutPointsAllCorridors() method generates data for all corridors.
-
The XSCutPointReporter class which extends DgnElementSetTool which handles different events to interact with UI, the XSCutPointReporter class overrides the events here in this tool .
-
The method OnDataButton() handles the code for corridor selection from UI.
Remarks
-
This sample code is a part of ManagedSDKExample which you get with SDK installation under "examples" section in SDK installation directory.
-
If you encounter any error while using DgnElementSetTool class, make sure to add a reference to Bentley.DgnDisplayNet.dll by selecting Project > Add Reference or change the projects .csproj file to add reference to this dll.
-
The default dll location will be "C:\Program Files\Bentley\OpenRoads Designer CE 10.11\OpenRoadsDesigner\Bentley.DgnDisplayNet.dll"
-
This code is simplified and does not include any form and report generation code, but user can get it under ManagedSDKExample ->Examples ->XSCutPointReporter
Source Code
using System;
using System.Collections.Generic;
using Bentley.DgnPlatformNET;
using Bentley.DgnPlatformNET.Elements;
using Bentley.CifNET.GeometryModel.SDK;
using Bentley.CifNET.SDK;
using Bentley.CifNET.Formatting;
namespace ManagedSDKExample
{
class XSCutPointReporter : DgnElementSetTool
{
private bool ALTERNATE_FORMAT = false;
private ConsensusConnection m_con;
public XSCutPointReporter() : base()
{
}
/*----------------------------------------------------------------------------------------------**/
/* Corridor Feature Report | For each corridor, reports the corridor's features.
/*--------------+---------------+---------------+---------------+---------------+----------------*/
internal void ReportAllXSCutPointsAllCorridors()
{
ConsensusConnection sdkCon = Bentley.CifNET.SDK.Edit.ConsensusConnectionEdit.GetActive();
if (sdkCon == null)
return;
GeometricModel geomModel = sdkCon.GetActiveGeometricModel();
if (geomModel == null)
return;
foreach (Corridor cor in geomModel.Corridors)
{
double length = cor.CorridorAlignment.LinearGeometry.Length;
AddXSCutPoints(cor, 0.0, length, true);
}
}
/*----------------------------------------------------------------------------------------------**/
/* Corridor Feature Report | For a single corridor selected by the user, reports the corridor's features.
/*--------------+---------------+---------------+---------------+---------------+----------------*/
internal void ReportAllXSCutPoints(Corridor cor, double startStation, double endStation)
{
//get report information
AddXSCutPoints(cor, startStation, endStation, false);
}
/*----------------------------------------------------------------------------------------------**/
/* Utility Function | Gets cut point information.
/*--------------+---------------+---------------+---------------+---------------+----------------*/
private void AddXSCutPoints(Corridor cor, double startStation, double endStation, bool allCorridors)
{
Dictionary<string, string> properties = new Dictionary<string, string>();
double station;
// ORD does all computations in meters.
// Therefore, input must be converted from Master units to Meters
double leftWidth = ConvertMasterToMeter(50);
double rightWidth = ConvertMasterToMeter(50);
double increment = ConvertMasterToMeter(cor.CorridorAlignment.LinearGeometry.Length);
if (!allCorridors)
{
startStation = ConvertMasterToMeter(0);
endStation = ConvertMasterToMeter(cor.CorridorAlignment.LinearGeometry.Length);
}
//alternative format
if (ALTERNATE_FORMAT)
{
for (station = startStation; station < endStation; station += increment)
{
if (station == 0)
station = 0.000001;
XSCutPoint[] points = cor.GetXSCutPoints(station, leftWidth, rightWidth, -leftWidth, Alignment.WhichFeatures.All, null);
if (points != null)
{
foreach (XSCutPoint pt in points)
{
properties.Add("FTR Name", pt.PointName);
properties.Add("FTR Defintion Name", pt.PointFeatureName);
properties.Add("Offset", FormatDistance(pt.Point.X));
properties.Add("Elevation", FormatDistance(pt.Point.Y));
properties.Add("X", FormatDistance(pt.PointOnPlan.X));
properties.Add("Y", FormatDistance(pt.PointOnPlan.Y));
properties.Add("Z", FormatDistance(pt.PointOnPlan.Z));
properties.Clear();
}
}
if (station == 0.000001)
station = 0.0;
}
}
//adds point information
else
{
for (station = startStation; station < endStation; station += increment)
{
if (station == 0)
station = 0.000001;
XSCutPoint[] points = cor.GetXSCutPoints(station, leftWidth, rightWidth, -leftWidth, Alignment.WhichFeatures.All, null);
if (points != null)
{
Array.Sort(points, (first, second) => first.Point.X.CompareTo(second.Point.X));
properties.Clear();
properties["FTR Name"] = "|";
properties["FTR Definition Name"] = "|";
properties["Offset"] = "|";
properties["Elevation"] = "|";
properties["X"] = "|";
properties["Y"] = "|";
properties["Z"] = "|";
foreach (XSCutPoint pt in points)
{
properties["FTR Name"] += pt.PointName + "|";
properties["FTR Definition Name"] += getCleanDefName(pt.PointFeatureName) + "|";
properties["Offset"] += FormatDistance(pt.Point.X) + "|";
properties["Elevation"] += FormatDistance(pt.Point.Y) + "|";
properties["X"] += FormatDistance(pt.PointOnPlan.X) + "|";
properties["Y"] += FormatDistance(pt.PointOnPlan.Z) + "|";
properties["Z"] += FormatDistance(pt.PointOnPlan.X) + "|";
}
}
if (station == 0.000001)
station = 0.0;
}
}
}
protected override void OnPostInstall()
{
base.BeginPickElements();
AccuSnap.LocateEnabled = true;
AccuSnap.SnapEnabled = true;
m_con = Bentley.CifNET.SDK.Edit.ConsensusConnectionEdit.GetActive();
if (m_con == null)
return;
base.OnPostInstall();
NotificationManager.OutputPrompt("Identify corridor.");
}
protected override bool OnPostLocate(HitPath path, out string cantAcceptReason)
{
//checks that hitpath is not null
if (path == null)
{
cantAcceptReason = "HitPath is null.";
return false;
}
//checks that the cursor element is not null
Element el = path.GetCursorElement();
if (el == null)
{
cantAcceptReason = "There is no element at cursor.";
return false;
}
//checks that the cursor element is a corridor
if (!(el.ElementType == (MSElementType)106))
{
cantAcceptReason = "This element is not a corridor.";
return false;
}
Corridor cor = (el.ParentElement == null) ? Corridor.CreateFromElement(m_con, el) : Corridor.CreateFromElement(m_con, el.ParentElement);
if (cor == null)
{
cantAcceptReason = "This element is not a corridor.";
return false;
}
cantAcceptReason = String.Empty;
return true;
}
protected override bool OnDataButton(DgnButtonEvent ev)
{
//gets corridor from the cursor element from hitpath
HitPath hitPath = DoLocate(ev, true, 1);
if (hitPath == null)
return false;
Element el = hitPath.GetCursorElement();
if (el == null)
return false;
Corridor cor = (el.ParentElement == null) ? Corridor.CreateFromElement(m_con, el) : Corridor.CreateFromElement(m_con, el.ParentElement);
if (cor == null)
{
NotificationManager.OutputPrompt("XSCutPoints only works with corridors.");
return false;
}
else
{
ReportAllXSCutPoints(cor, 0, cor.CorridorAlignment.LinearGeometry.Length);
return true;
}
}
private string getCleanDefName(string filePath)
{
int index = filePath.LastIndexOf('\\');
if (index < 0)
{
return filePath;
}
return filePath.Substring(index + 1);
}
protected override void OnRestartTool()
{
InstallNewInstance();
}
protected override void ExitTool()
{
if (m_con != null)
{
m_con.Close();
m_con.Dispose();
}
m_con = null;
base.ExitTool();
}
public override StatusInt OnElementModify(Element element)
{
return StatusInt.Error;
}
public static void InstallNewInstance()
{
XSCutPointReporter tool = new XSCutPointReporter();
tool.InstallTool();
}
protected double ConvertMasterToMeter(double num)
{
double defaultUnitsToMeters = FormatSettingsConstants.GetMasterUnitsToMeters();
return num * defaultUnitsToMeters;
}
protected string FormatDistance(double value)
{
DgnModel activeModel = Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModel();
return FormatForDisplay.Distance(value, activeModel, 4);
}
}
}